﻿
namespace Patterns4Net.Core.CecilExtensions.CILPatternsMatching
{
    using System;
    using System.Collections.Generic;
    using Mono.Cecil.Cil;

    public class OpCodeInstructionMatcher : PredicateInstructionMatcher
    {
        private readonly OpCode opCode;

        public OpCodeInstructionMatcher(OpCode opCode)
            : base(i => i.OpCode == opCode)
        {
            this.opCode = opCode;
        }

        public OpCodeInstructionMatcher(OpCode opCode, Func<object, bool> operandPredicate)
            : base(i => i.OpCode == opCode && operandPredicate.Invoke(i.Operand))
        {
            this.opCode = opCode;
        }

        public static OpCodeInstructionMatcher[] Create(params OpCode[] opCodes)
        {
            var result = new List<OpCodeInstructionMatcher>();
            foreach (var opCode in opCodes)
            {
                result.Add(new OpCodeInstructionMatcher(opCode));
            }

            return result.ToArray();
        }

        public override string ToString()
        {
            return this.opCode.ToString();
        }
    }
}
